package org.example.fancystudy.Lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author chaigq
 * @version 1.0
 * @date 2022/11/9 21:15
 */
public class SaleTicketDemo {
    public static void main(String[] args) {
        Tick tick = new Tick();

        new Thread(() -> {
            for (int i = 0; i <= 55; i++){
                tick.sale();
            }
        }, "a").start();

        new Thread(() -> {
            for (int i = 0; i <= 55; i++){
                tick.sale();
            }
        }, "b").start();

        new Thread(() -> {
            for (int i = 0; i <= 55; i++){
                tick.sale();
            }
        }, "c").start();
    }
}
class Tick{
    private int number = 50;
    //ReentrantLock reentrantLock = new ReentrantLock();
    ReentrantLock reentrantLock = new ReentrantLock(true);
    public void sale(){
        reentrantLock.lock();
        try {
            if (number > 0){
                System.out.println(Thread.currentThread().getName() + "卖出第:\t" + number -- + "\t" + "还剩下：" + number);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            reentrantLock.unlock();
        }
    }
}
